<?php 
/**
 * 签名类
 * @author liuyang
 *
 */
namespace Common\Util;
class SignUtils {
    
    const SIGN_KEY="DODWOW_@)!%&*SDFSFHSKFDOY&132IOOWHEOIWUHEFWIPEFHWEFSFJHFEW12131";
    const EXPIRE_TIME=1800;
    /**
     * 格式化参数格式化成url参数
     */
    public static function toUrlParams($data)
    {
        $buff = ""; 
        foreach ($data as $k => $v)
        { 
            if($k != "sign"  && !is_array($v)){ 
                $buff .= $k . "=" . $v . "&"; 
            }
        }
    
        $buff = trim($buff, "&"); 
        return $buff;
    }
    
    /**
     * 生成签名
     * @return 签名，本函数不覆盖sign成员变量，如要设置签名需要调用SetSign方法赋值
     */
    public static function makeSign($data=array())
    {
        $data['key']=self::SIGN_KEY; 
        //签名步骤一：按字典序排序参数
        ksort($data);
        $string = self::toUrlParams($data); 
        //签名步骤二：MD5加密
        $string = md5($string);
        //签名步骤三：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    } 
    
    public static function validateSign(){
        $data = $_REQUEST;
        unset($data['_URL_']); 
        $sign = $data['sign']; 
        $mySign = self::makeSign($data);  
        if($sign===$mySign){
            return true;
        }else{
            return false;
        }
        
    }
}
?>